package com.aorise.service.halt.impl;

import com.aorise.exception.ServiceException;
import com.aorise.mapper.halt.*;
import com.aorise.model.halt.*;
import com.aorise.service.camera.CameraGroupService;
import com.aorise.service.halt.HaltService;
import com.aorise.utils.ConstDefine;
import com.aorise.utils.StatusDefine;
import com.aorise.utils.StatusDefineMessage;
import com.aorise.utils.page.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:cat
 * @Desicription: 停机记录接口服务实现类
 * @Date:Created in 2019-06-11
 * @Modified By:
 */
@Service
public class HaltServiceImpl implements HaltService {

    @Autowired
    private HaltMapper haltMapper;
    @Autowired
    private HaltCameraMapper haltCameraMapper;
    @Autowired
    private HaltInformMapper haltInformMapper;
    @Autowired
    private HaltCommentMapper haltCommentMapper;
    @Autowired
    private HaltLikesMapper haltLikesMapper;
    @Autowired
    private CameraGroupService cameraGroupService;

    /**
     * @Author:cat
     * @Description 分页查询停机记录信息
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-06-11
     * @Return: List<HaltModel> 停机记录信息集合
     * @Modified By:
     */
    @Override
    public List<HaltModel> getAllHaltListByPage(Map<String, Object> map, Page page) {
        List<HaltModel> haltModels = null;

        /**查询总条数*/
        Long count = haltMapper.getAllHaltListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                haltModels = haltMapper.getAllHaltListByPage(map);
            }
        } else {
            page.setTotalCount(0L);
        }
        return haltModels;
    }

    /**
     * @Author:cat
     * @Description 根据ID查询停机记录详情
     * @params: id 主键ID
     * @params: userId 登录人ID
     * @Date:2019-06-11
     * @Return: HaltModel 停机记录信息
     * @Modified By:
     */
    @Override
    public HaltModel getHaltById(Integer id,Integer userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("userId", userId);
        map.put("isReadNo", ConstDefine.ISREAD_NO);
        map.put("isRead", ConstDefine.ISREAD);
        map.put("isLikeNo", ConstDefine.ISLIKE_NO);
        map.put("isLike", ConstDefine.ISLIKE);
        map.put("state", ConstDefine.STATE_ABLE);
        HaltModel haltModel = haltMapper.getHaltById(map);

        if (haltModel != null) {
            /**查询停机设备*/
            Map<String, Object> mapM = new HashMap<>();
            mapM.put("haltId", haltModel.getId());
            mapM.put("state", ConstDefine.STATE_ABLE);
            List<HaltCameraModel> haltCameraModels = haltCameraMapper.getHaltCameraByHaltId(mapM);
            if (haltCameraModels.size() > 0) {
                for (int i = 0; i < haltCameraModels.size(); i++) {
                    if (haltCameraModels.get(i).getGroupId() != null && haltCameraModels.get(i).getGroupId() != 0) {
                        String groupName =cameraGroupService.getCameraGroupNameById(haltCameraModels.get(i).getGroupId());
                        haltCameraModels.get(i).setGroupName(groupName);
                    }
                }
                haltModel.setHaltCameraModels(haltCameraModels);
            }

            /**如果停机记录为未读消息，修改状态为已读*/
            if(haltModel.getIsRead()==ConstDefine.ISREAD_NO){
                Map<String, Object> mapI = new HashMap<>();
                mapI.put("haltId", id);
                mapI.put("userId", userId);
                haltInformMapper.deleteHaltInform(mapI);
            }

            /**查询评论记录*/
            Map<String, Object> mapC = new HashMap<>();
            mapC.put("haltId", haltModel.getId());
            List<HaltCommentModel> haltCommentModels =  haltCommentMapper.getHaltCommentByHaltId(mapC);

            haltModel.setHaltCommentModels(haltCommentModels);

        }
        return haltModel;
    }

    /**
     * @Author:cat
     * @Description 新增停机记录信息
     * @params: organAddVO 停机记录信息
     * @Date:2019-06-11
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    @Transactional
    public Integer addHalt(HaltAddVO haltAddVO) {
        Integer iRet = haltMapper.addHalt(haltAddVO);
        int id = 0;
        if (iRet > 0) {
            /**新增停机设备信息*/
            if (!StringUtils.isBlank(haltAddVO.getCameraIds())) {
                String cameraIds[] = haltAddVO.getCameraIds().split(",");

                for (int i = 0; i < cameraIds.length; i++) {
                    HaltCameraAddVO haltCameraAddVO = new HaltCameraAddVO();
                    haltCameraAddVO.setHaltId(haltAddVO.getId());
                    haltCameraAddVO.setCameraId(Integer.parseInt(cameraIds[i]));
                    haltCameraMapper.addHaltCamera(haltCameraAddVO);
                }
                id = haltAddVO.getId();
            }else {
                throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.CAMERA_NULL));
            }
        }
        return id;
    }

    /**
     * @Author:cat
     * @Description 删除停机记录信息
     * @params: map 查询条件Map
     * @Date:2019-06-11
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer deleteHalt(Map<String, Object> map) {
        /**判断是否为待审批的申请*/
        Map<String, Object> mapM = new HashMap<>();
        mapM.put("id", map.get("id"));
        mapM.put("userId", map.get("editUser"));
        mapM.put("isReadNo", ConstDefine.ISREAD_NO);
        mapM.put("isRead", ConstDefine.ISREAD);
        mapM.put("isLikeNo", ConstDefine.ISLIKE_NO);
        mapM.put("isLike", ConstDefine.ISLIKE);
        mapM.put("state", ConstDefine.STATE_ABLE);
        HaltModel haltModel = haltMapper.getHaltById(mapM);
        if(haltModel.getStatus()!=ConstDefine.APPROVE_STATUS_WAIT) {
            throw new ServiceException(StatusDefineMessage.GetMessage(StatusDefine.APPLICATION_DELETE));
        }

        return haltMapper.deleteHalt(map);
    }

    /**
     * @Author:cat
     * @Description 查询停机记录未读消息总数
     * @params: map 查询条件
     * @Date:2019-06-11
     * @Return: Long 数据总条数
     * @Modified By:
     */
    @Override
    public Long getHaltIsNotRead(Map<String, Object> map) {

        return haltInformMapper.getHaltInformByUserId(map);
    }

    /**
     * @Author:cat
     * @Description 对停机记录信息点赞、取消赞
     * @params: map 查询条件Map
     * @Date:2019-06-11
     * @Return: Integer 影响行数
     * @Modified By:
     */
    @Override
    public Integer doLikeHalt(Map<String, Object> map) {
        /**判断是否已点赞*/
        Map<String, Object> mapL = new HashMap<>();
        mapL.put("haltId", map.get("haltId"));
        mapL.put("userId", map.get("userId"));
        HaltLikesModel haltLikesModelTemp = haltLikesMapper.getHaltLikesByHaltIdUserId(mapL);
        if (haltLikesModelTemp == null) {
            /**新增点赞信息*/
            HaltLikesModel haltLikesModel =new HaltLikesModel();
            haltLikesModel.setHaltId(Integer.parseInt(map.get("haltId").toString()));
            haltLikesModel.setUserId(Integer.parseInt(map.get("userId").toString()));
            return haltLikesMapper.addHaltLikes(haltLikesModel);
        } else {
            /**删除点赞信息*/
            return haltLikesMapper.deleteHaltLikes(map);
        }
    }

    /**
     * @Author:cat
     * @Description 新增停机记录评论信息
     * @params: haltCommentAddVO 停机记录评论信息
     * @Date:2019-06-11
     * @Return: Integer 主键
     * @Modified By:
     */
    @Override
    public Integer addHaltComment(HaltCommentAddVO haltCommentAddVO) {

        return haltCommentMapper.addHaltComment(haltCommentAddVO);
    }

    /**
     * @Author:cat
     * @Description 根据条件分页查询停机设备
     * @params: map 查询条件
     * @params: page 分页
     * @Date:2019-06-12
     * @Return: List<HaltCameraModel> 停机设备集合
     * @Modified By:
     */
    @Override
    public List<HaltCameraModel> getHaltCameraListByPage(Map<String, Object> map, Page page) {
        List<HaltCameraModel> haltCameraModels = null;

        /**查询总条数*/
        Long count = haltCameraMapper.getHaltCameraListTotalByPage(map);
        if (count > 0) {
            page.setTotalCount(count);
            page.setTotalPage(count, page.getEveryPage());
            if (page.getCurrentPage() > 0 && page.getEveryPage() > 0) {
                /**查询分页条数*/
                haltCameraModels = haltCameraMapper.getHaltCameraListByPage(map);
                for (int i = 0; i < haltCameraModels.size(); i++) {
                    if(haltCameraModels.get(i).getGroupId() != null && haltCameraModels.get(i).getGroupId() != 0){
                        String groupName =cameraGroupService.getCameraGroupNameById(haltCameraModels.get(i).getGroupId());
                        haltCameraModels.get(i).setGroupName(groupName);
                    }
                }
            }
        } else {
            page.setTotalCount(0L);
        }
        return haltCameraModels;
    }
}
